#include "userinfowidget.h"
#include "mainwidget.h"
#include "model/data.h"
#include <QGridLayout>
#include <QMessageBox>
#include <QPushButton>

UserInfoWidget::UserInfoWidget(const model::UserInfo &userInfo, QWidget *parent) : QDialog(parent), _userInfo(userInfo)
{
    // 1. 设置基本属性
    this->setFixedSize(400, 200);
    this->setWindowTitle("用户详细");
    this->setWindowIcon(QIcon(":.resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->move(QCursor::pos());

    // 2. 创建布局管理器
    QGridLayout *layout = new QGridLayout();
    layout->setVerticalSpacing(10);
    layout->setHorizontalSpacing(20);
    layout->setContentsMargins(40, 20, 0, 0);
    layout->setAlignment(Qt::AlignTop);
    this->setLayout(layout);

    // 3. 添加头像
    _avatarBtn = new QPushButton();
    _avatarBtn->setFixedSize(75, 75);
    _avatarBtn->setIconSize(QSize(75, 75));
    _avatarBtn->setIcon(_userInfo.avatar);

    QString labelStyle = "QLabel { font-weight: 800; padding-left: 20px;}";
    QString btnStyle   = "QPushButton { border: 1px solid rgb(100, 100, 100); border-radius: 5px; background-color: "
                         "rgb(240, 240, 240); }";
    btnStyle += "QPushButton:pressed { background-color: rgb(205, 205, 205); }";

    int width  = 80;
    int height = 30;

    // 4. 添加用户序号
    _idTag = new QLabel();
    _idTag->setText("序号");
    _idTag->setStyleSheet(labelStyle);
    _idTag->setFixedSize(width, height);
    _idTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    _idLabel = new QLabel();
    _idLabel->setText(_userInfo.userId);
    _idLabel->setFixedSize(width, height);
    _idLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 5. 添加用户昵称
    _nameTag = new QLabel();
    _nameTag->setText("昵称");
    _nameTag->setStyleSheet(labelStyle);
    _nameTag->setFixedSize(width, height);
    _nameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    _nameLabel = new QLabel();
    _nameLabel->setText(_userInfo.nickname);
    _nameLabel->setFixedSize(width, height);
    _nameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 6. 设置电话
    _phoneTag = new QLabel();
    _phoneTag->setText("电话");
    _phoneTag->setStyleSheet(labelStyle);
    _phoneTag->setFixedSize(width, height);
    _phoneTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    _phoneLabel = new QLabel();
    _phoneLabel->setText(_userInfo.phone);
    _phoneLabel->setFixedSize(width, height);
    _phoneLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 7. 添加功能按钮
    _applyBtn = new QPushButton();
    _applyBtn->setText("申请好友");
    _applyBtn->setStyleSheet(btnStyle);
    _applyBtn->setFixedSize(80, 30);
    _applyBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    _sendMessageBtn = new QPushButton();
    _sendMessageBtn->setText("发送消息");
    _sendMessageBtn->setStyleSheet(btnStyle);
    _sendMessageBtn->setFixedSize(80, 30);
    _sendMessageBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    _deleteFriendBtn = new QPushButton();
    _deleteFriendBtn->setText("删除好友");
    _deleteFriendBtn->setStyleSheet(btnStyle);
    _deleteFriendBtn->setFixedSize(80, 30);
    _deleteFriendBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);

    // 8. 添加到布局管理器
    layout->addWidget(_avatarBtn, 0, 0, 3, 1);

    layout->addWidget(_idTag, 0, 1);
    layout->addWidget(_idLabel, 0, 2);

    layout->addWidget(_nameTag, 1, 1);
    layout->addWidget(_nameLabel, 1, 2);

    layout->addWidget(_phoneTag, 2, 1);
    layout->addWidget(_phoneLabel, 2, 2);

    layout->addWidget(_applyBtn, 3, 0);
    layout->addWidget(_sendMessageBtn, 3, 1);
    layout->addWidget(_deleteFriendBtn, 3, 2);

    // 9. 初始化按钮的禁用关系
    // 当双方是好友时，申请好友按钮为禁用状态，发送消息按钮为禁用状态，删除好友为可用状态，以此类推
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    auto *myFriend                = dataCenter->findFriendById(this->_userInfo.userId);
    if (myFriend == nullptr)
    {
        // 不是好友
        _sendMessageBtn->setEnabled(false);
        _deleteFriendBtn->setEnabled(false);
    }
    else
    {
        // 是好友
        _applyBtn->setEnabled(false);
    }

    // 10 初始化信号槽
    initSignalSlot();
}

void UserInfoWidget::initSignalSlot()
{
    connect(_sendMessageBtn, &QPushButton::clicked, this,
            [=]()
            {
                // 通过主窗口的 switchSession 函数跳转到对应好友会话窗口
                MainWidget *mainWidget = MainWidget::getInstance();
                mainWidget->switchSession(_userInfo.userId);

                // 把本窗口关闭
                this->close();
            });

    connect(_deleteFriendBtn, &QPushButton::clicked, this, &UserInfoWidget::clickDeleteFriendBtn);
    connect(_applyBtn, &QPushButton::clicked, this, &UserInfoWidget::clickApplyBtn);
}

void UserInfoWidget::clickDeleteFriendBtn()
{
    // 弹出对话框，让用户确认是否删除好友
    auto result = QMessageBox::warning(this, "确认删除", "确认删除当前好友？", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok)
    {
        LOG() << "删除好友取消";
        return;
    }

    // 2. 发送网络请求，实现删除好友
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    dataCenter->deleteFriendAsync(_userInfo.userId);
    this->close();
}

void UserInfoWidget::clickApplyBtn()
{
    // 发送好友申请
    model::DataCenter *dataCenter = model::DataCenter::getInstance();
    dataCenter->addFriendApplyAsync(_userInfo.userId);
    this->close();
}
